home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Source Code / Visual Basic Source Code.iso / vbsource / tsplit / hsplit.cls < prev    next >
Encoding:
Text File  |  1995-10-01  |  6.8 KB  |  222 lines

  1. VERSION 1.0 CLASS
  2. BEGIN
  3.   MultiUse = -1  'True
  4. END
  5. Attribute VB_Name = "CHSplitter"
  6. Attribute VB_Creatable = False
  7. Attribute VB_Exposed = False
  8. Option Explicit
  9.  
  10. ' Internal variables for forms and controls
  11. Private ctlTop As Control
  12. Private ctlBottom As Control
  13. Private objContainer As Object
  14.  
  15. ' Sizes of borders and pixels
  16. Private ySplit As Single
  17. Private dySplit As Single
  18. Private xPixel As Single
  19. Private yPixel As Single
  20. Private dxBorder As Single
  21. Private dyBorder As Single
  22.  
  23. ' Flags
  24. Private fResize As Boolean
  25. Private fAutoBorder As Boolean
  26. Private fDragging As Boolean
  27. Private fDragIcon As Boolean
  28. Private fCreated As Boolean
  29.  
  30. ' Old mouse pointer, draw style, and draw mode
  31. Private mpOld As Integer
  32. Private dsOld As Integer
  33. Private dmOld As Integer
  34.  
  35. ' AutoRedraw
  36. Private arOld As Boolean
  37.  
  38. ' Create a splitter window
  39. Function Create(vTopControl As Control, vBottomControl As Control, _
  40.                 Optional vBorderPixels As Variant, _
  41.                 Optional vAutoBorder As Variant, _
  42.                 Optional vResizeable As Variant) As Boolean
  43.                 
  44.     Create = True
  45.     fCreated = False
  46.     On Error GoTo CreateError
  47.     ' Set internal controls
  48.     Set ctlTop = vTopControl
  49.     Set ctlBottom = vBottomControl
  50.     Set objContainer = ctlTop.Container
  51.     objContainer.AutoRedraw = True
  52.     If objContainer.ClipControls Then GoTo CreateError
  53.     
  54.     ' Save resizable and AutoBorder flags
  55.     If IsMissing(vResizeable) Then vResizeable = True
  56.     fResize = vResizeable
  57.     If IsMissing(vAutoBorder) Then vAutoBorder = True
  58.     fAutoBorder = vAutoBorder
  59.     
  60.     ' Size of one in pixel in current scale
  61.     xPixel = objContainer.ScaleX(1, vbPixels, objContainer.ScaleMode)
  62.     yPixel = objContainer.ScaleY(1, vbPixels, objContainer.ScaleMode)
  63.     ' Set splitter size
  64.     If IsMissing(vBorderPixels) Then
  65.         fAutoBorder = True
  66.         vBorderPixels = 4
  67.     End If
  68.     dySplit = vBorderPixels * yPixel
  69.     ' Set border size
  70.     If fAutoBorder Then
  71.         dxBorder = ctlTop.Left
  72.         dyBorder = ctlTop.Top
  73.     Else
  74.         dxBorder = vBorderPixels * xPixel
  75.         dyBorder = vBorderPixels * yPixel
  76.     End If
  77.  
  78.     ' Size the controls
  79.     If ctlBottom.Top < ctlTop.Top Then GoTo CreateError
  80.     If yBottom(ctlBottom) < yBottom(ctlTop) Then GoTo CreateError
  81.     Resize
  82.     fCreated = True
  83.     Exit Function
  84.     
  85. CreateError:
  86.     Create = False
  87. End Function
  88.  
  89. Sub Resize()
  90.  
  91.     ' Move everything in border size from the edge
  92.     ctlTop.Left = dxBorder
  93.     ctlTop.Top = objContainer.ScaleTop + dyBorder
  94.     ctlTop.Width = objContainer.ScaleWidth - (2 * dxBorder)
  95.     ' ctlTop.Height ' Unchanged
  96.         
  97.     ctlBottom.Left = dxBorder
  98.     ctlBottom.Top = yBottom(ctlTop) + dySplit
  99.     ctlBottom.Width = ctlTop.Width
  100.     ctlBottom.Height = objContainer.ScaleHeight - ctlBottom.Top - dyBorder
  101.  
  102. End Sub
  103.  
  104. Sub HSplitter_MouseMove(Button As Integer, Shift As Integer, _
  105.                         X As Single, Y As Single)
  106. With objContainer
  107.     If Not fCreated Then Exit Sub
  108.     Dim yPos As Single
  109.     ' Change the cursor to splitter or back
  110.     If Y <= ctlBottom.Top And Y >= yBottom(ctlTop) Then
  111.         If .MousePointer <> 99 And .MousePointer <> vbSizeNS Then
  112.             mpOld = .MousePointer
  113.             If .MouseIcon.Type <> vbPicTypeIcon Then
  114.                 .MousePointer = vbSizeNS
  115.             Else
  116.                 .MousePointer = 99
  117.             End If
  118.         End If
  119.     Else
  120.         If (.MousePointer = 99 Or .MousePointer = vbSizeNS) _
  121.            And Button <> vbLeftButton Then
  122.             .MousePointer = mpOld
  123.         End If
  124.     End If
  125.     
  126.     ' Move the splitter line if within range
  127.     If fDragging And (ySplit <> Y) And _
  128.        (Y > (yPixel * 20)) And (Y < (.ScaleWidth - (yPixel * 40))) Then
  129.         .DrawStyle = vbInsideSolid
  130.         .DrawMode = vbInvert
  131.         yPos = ySplit
  132.         ' Erase old line
  133.         objContainer.Line (ctlTop.Left, yPos - yPixel)-(ctlTop.Width, yPos + yPixel), , B
  134.         ' Draw new line
  135.         yPos = Y
  136.         objContainer.Line (ctlTop.Left, yPos - yPixel)-(ctlTop.Width, yPos + yPixel), , B
  137.         ySplit = yPos
  138.     End If
  139. End With
  140. End Sub
  141.  
  142. ' Put in MouseMove of the contained controls
  143. Sub HSplitter_MouseOff()
  144. With objContainer
  145.     If Not fCreated Then Exit Sub
  146.     If .MousePointer = 99 Or .MousePointer = vbSizeWE Then .MousePointer = mpOld
  147. End With
  148. End Sub
  149.  
  150. Sub HSplitter_MouseDown(Button As Integer, Shift As Integer, _
  151.                         X As Single, Y As Single)
  152. With objContainer
  153.     If Not fCreated Then Exit Sub
  154.     Dim yPos As Single
  155.     yPos = yBottom(ctlTop)
  156.     ' If over splitter start a drag
  157.     If (yPos < Y) And (Y < ctlBottom.Top) Then
  158.         If (Button = vbLeftButton) And (yPos < Y) And (Y < ctlBottom.Top) Then
  159.             ' Save and restore state
  160.             fDragging = True
  161.             dsOld = .DrawStyle
  162.             dmOld = .DrawMode
  163.             arOld = .AutoRedraw
  164.             .DrawStyle = vbInsideSolid
  165.             .DrawMode = vbInvert
  166.             .AutoRedraw = False
  167.             ' Draw the splitter line and save position
  168.             yPos = yPos + (dxBorder / 3)
  169.             objContainer.Line (ctlTop.Left, yPos - yPixel)-(ctlTop.Width, yPos + yPixel), , B
  170.             ySplit = yPos
  171.         End If
  172.     Else
  173.         If .MousePointer = 99 Or .MousePointer = vbSizeWE Then .MousePointer = mpOld
  174.     End If
  175. End With
  176. End Sub
  177.  
  178. Sub HSplitter_MouseUp(Button As Integer, Shift As Integer, _
  179.                       X As Single, Y As Single)
  180. With objContainer
  181.     Dim yPos As Single
  182.     If Not fCreated Then Exit Sub
  183.     If fDragging Then
  184.         ' Erase old line
  185.         .DrawStyle = vbInsideSolid
  186.         .DrawMode = vbInvert
  187.         yPos = ySplit
  188.         objContainer.Line (ctlTop.Left, yPos - yPixel)-(ctlTop.Width, yPos + yPixel), , B
  189.         .DrawStyle = dsOld
  190.         .DrawMode = dmOld
  191.         fDragging = False
  192.         ' Resize the panes if in range
  193.         If Y > (yPixel * 20) And Y < (.ScaleHeight - (yPixel * 20)) Then
  194.             ctlTop.Height = Y - ctlTop.Top - (dySplit / 2)
  195.             ctlBottom.Top = yBottom(ctlTop) + dySplit
  196.             ctlBottom.Height = .ScaleHeight - ctlBottom.Top - dyBorder
  197.         End If
  198.         .DrawStyle = dsOld
  199.         .DrawMode = dmOld
  200.         .AutoRedraw = arOld
  201.     End If
  202. End With
  203. End Sub
  204.  
  205. Sub HSplitter_Resize()
  206.     If objContainer Is Nothing Then Exit Sub
  207.     If Not fCreated Then Exit Sub
  208.     ' Only forms have WindowState
  209.     On Error Resume Next
  210.     If objContainer.WindowState <> vbMinimized And fResize Then Resize
  211.     ' Must not be form
  212.     If Err And fResize Then Resize
  213. End Sub
  214.  
  215. Private Function xRight(obj As Object) As Single
  216.     xRight = obj.Left + obj.Width
  217. End Function
  218.  
  219. Private Function yBottom(obj As Object) As Single
  220.     yBottom = obj.Top + obj.Height
  221. End Function
  222.